# -*- coding:utf-8 -*-
'''
对word文档进行转换，保存在txt文件；
在转换过程中对中文大写等转换成罗马数字
'''

import docx2txt
# from docx import Document
import hashlib
import os
import re
import pickle
from companyName_2 import startEnd
from unitSlice import FileOperation
from myUtil import replacing,handleChengduiAndOtherFlag,isOther,delCommonWordsSpace,isTelphoneNumber,isMobilphoneNumber,isAddress,isEmail,isQq,isUrl,isCompanyName,isWeichat,splitLine,isContact,isIncludeTelNumber,isIncludePhoneNumber,makeDir
from demo.config import itemdir,regionFile,wordManyInfoDir,customInfoDir

pkl_file = open(regionFile, 'rb')
regionInfo = pickle.load(pkl_file)
pkl_file.close()
class Word2Txt(object):

    @classmethod
    def getMd5(cls,string):
        myMd5 = hashlib.md5()
        myMd5.update(string)
        myMd5_Digest = myMd5.hexdigest()
        return myMd5_Digest
    def readWord(self,file):
        '''
        :param file:
        :return:返回word文档中的文本信息，并且对文本信息进行预处理，按行进行分隔
        '''
        # 在对word文档的内容进行处理时实现分行

        text = docx2txt.process(file)   #step-1

        res=[]
        for each in text.split("\n"):   #按照显式进行分行
            lineTxt=each.strip()
            if len(lineTxt)!=0:    #删除空行   step-2
                # 进行常用词中空格替换操作
                # lineTxt=delCommonWordsSpace(lineTxt,commonWordsInfo)
                temp=replacing(lineTxt)    #step-3完成全角2半角、汉字数字2阿拉伯、中文大写2阿拉伯、空格处理，英文大写转小写，temp是utf-8编码

                step1res=handleChengduiAndOtherFlag(unicode(temp))  #step-4-1 完成对成对符、其他符号的处理step1res是unicode
                guodu=splitLine(step1res)     #step-4-2 完成对常用词处理、分行
                res+=guodu
            else:
                continue
        return res

    def word2Txt(self,dirPath,inputfile):
        res=self.readWord(dirPath+inputfile)
        outputFileName="F:/target/txt/"+Word2Txt.getMd5(inputfile)+".txt"
        output=open(outputFileName,'a')

        for line in res:
            # 去除word中的空行
            if len(line.strip())==0:
                continue
            line=replacing(line.strip())   #不需要判断全部进行全角、中文、空格等数据的转换操作
            output.write(line)
            output.write("\n")
            output.flush()
        output.close()
    def word2FileSlice(self,dirPath,inputfile,filemd5):
        '''
        处理多个单文件、单公司信息——批量处理
        将word文件中的内容进行分片处理
        :return:
        '''
        # 获取文件操作对象
        # xxx/output目录
        fileObj=FileOperation(itemdir,filemd5)

        qqFile=None
        weichatFile=None
        emailFile=None
        addressFile=None
        phoneFile=None
        telFile=None
        urlFile=None
        nameFile=None
        otherFile=None


        res=self.readWord(dirPath+inputfile)
        flag=None

        # pkl_file = open(regionFile, 'rb')
        # regionInfo = pickle.load(pkl_file)
        # pkl_file.close()

        for line in res:
            #处理公司名称
            obj=startEnd()
            if isCompanyName(line,obj,regionInfo):
                if nameFile==None:
                    nameFile=fileObj.fileCreate('companyName')
                nameFile.write(obj.outputCompanyName(line))
                nameFile.write("\n")
                continue
            each=line
            if isQq(each):
                if qqFile==None:
                    qqFile=fileObj.fileCreate('qq')
                qqFile.write("\n")
                qqFile.write(each)
                # qqFile.write(";")
                flag=qqFile
            elif isWeichat(each):
                if weichatFile==None:
                    weichatFile=fileObj.fileCreate('weichat')
                weichatFile.write("\n")
                weichatFile.write(each)
                # weichatFile.write(";")
                flag=weichatFile
            elif isEmail(each):
                if emailFile==None:
                    emailFile=fileObj.fileCreate('email')
                emailFile.write("\n")
                emailFile.write(each)
                # emailFile.write(";")
                flag=emailFile
            elif isAddress(each):
                if addressFile==None:
                    addressFile=fileObj.fileCreate('address')
                addressFile.write("\n")
                addressFile.write(each)
                # addressFile.write(";")
                flag=addressFile
            elif isMobilphoneNumber(each):
                if phoneFile==None:
                    phoneFile=fileObj.fileCreate('phone')
                phoneFile.write("\n")
                phoneFile.write(each)
                # phoneFile.write(";")
                flag=phoneFile
            elif isTelphoneNumber(each):
                if telFile==None:
                    telFile=fileObj.fileCreate('telphone')
                telFile.write("\n")
                telFile.write(each)
                # telFile.write(";")
                flag=telFile
            elif isUrl(each):
                if urlFile==None:
                    urlFile=fileObj.fileCreate('url')
                urlFile.write("\n")
                urlFile.write(each)
                # urlFile.write(" ")
                flag=urlFile
            elif isOther(each):
                if otherFile==None:
                    otherFile=fileObj.fileCreate('other')
                otherFile.write('\n')
                otherFile.write(each)
                flag=otherFile

            else:
                if flag !=None:
                    flag.write("\n")
                    flag.write(each)
                    # flag.write(";")
                # else:
                #     continue
        #文件关闭操作
        fileObj.fileClose(qqFile)
        fileObj.fileClose(addressFile)
        fileObj.fileClose(weichatFile)
        fileObj.fileClose(emailFile)
        fileObj.fileClose(phoneFile)
        fileObj.fileClose(telFile)
        fileObj.fileClose(urlFile)
        fileObj.fileClose(nameFile)

    def word2CompanyInfoSegment(self,dirpath,inputfile,parentDirMd5):
        #从word中读取信息，并对公司信息进行分块
        context=self.readWord(dirpath+inputfile)     #获取到word文档分行后的结果，是一个列表

        # pkl_file = open(regionFile, 'rb')
        # regionInfo = pickle.load(pkl_file)
        # pkl_file.close()

        count=1
        segRecord={}
        target=None
        temp=[]    #temp数据结构：[开始行号，结束行号，公司名称]
        # 记录文件中的行数和每一行的内容
        # -------------------------------------------------
        # for循环用于记录文档中每一家公司的开头和结束位置，目前主要处理公司名称在后而联系方式在前的情况
        start=0
        end=0

        for i,rowValue in enumerate(context):

            obj=startEnd()
            if isCompanyName(rowValue,obj,regionInfo):
                companyName=obj.outputCompanyName(rowValue)
                linshi=[companyName,i]

                if target ==None:
                    target=linshi
                    # end=linshi[1]
                    # start=i+1
                elif target[0] != companyName:
                    temp.append(start)
                    temp.append(target[1])
                    temp.append(Word2Txt.getMd5(target[0]))
                    segRecord[count]=temp
                    count+=1
                    start=target[1]+1
                    target=linshi
                    temp=[]
                else:
                    target[1]=i
                if i == (len(context)-1):
                    temp.append(start)
                    temp.append(i)
                    temp.append(Word2Txt.getMd5(target[0]))
                    segRecord[count]=temp
                else:
                    continue
            else:
                continue

        # 根据计算出的位置进行分块处理

        self.sliceOutputCompanyInfo(segRecord,context,parentDirMd5,regionInfo)

    def sliceOutputCompanyInfo(self,segOrder,context,parentMd5,regionInfo):
        count=len(segOrder)
        if count !=0:
            for i in xrange(count):
                try:
                    sliceInfo=segOrder[i+1]   #取出字典中的一个位置信息块
                    start=sliceInfo[0]
                    end=sliceInfo[1]
                    singleCompanyMd5=sliceInfo[2]
                    res=context[start:end+1]   #把原始文档中的目标行之间的数据进行存储
                    # 需要对输出信息块中的信息进行初步过滤，按照标准名称进行计数
                    if self.countMark(res):
                        # 对分块信息进行输出存储
                        self.outputInfo(res,parentMd5,singleCompanyMd5,"fullinfo",regionInfo)
                except:
                    continue
        else:
            return None

    def outputInfo(self,listInfo,parentMd5,singleInfomd5,classic,regionInfo):
        # 将每一个分块信息进行存储
        parentDir=wordManyInfoDir+"\\"+parentMd5
        # 再次创建文件夹
        makeDir(parentDir,singleInfomd5)
        companyDir=parentDir+'\\'+singleInfomd5
        fileObj=FileOperation(companyDir,"")
        fh=fileObj.fileCreate(classic)
        for each in listInfo:
            fh.write(each)
            fh.write("\n")
        fileObj.fileClose(fh)
        #对公司信息块进行分单元处理
        self.outputInfoFenpian(companyDir,singleInfomd5,regionInfo,listInfo)

    def outputInfoFenpian(self,handleDir,companyMd5,regionInfo,listInfo):
        fileObj=FileOperation(handleDir,companyMd5)
        qqFile=None
        weichatFile=None
        emailFile=None
        addressFile=None
        phoneFile=None
        telFile=None
        urlFile=None
        nameFile=None
        otherFile=None
        flag=None

        for line in listInfo:
            #处理公司名称
            obj=startEnd()
            if isCompanyName(line,obj,regionInfo):
                if nameFile==None:
                    nameFile=fileObj.fileCreate('companyName')

                nameFile.write(obj.outputCompanyName(line))
                nameFile.write("\n")
                continue

            each=line
            # ----------------------------------------------
            if isQq(each):
                if qqFile==None:
                    qqFile=fileObj.fileCreate('qq')
                qqFile.write("\n")
                qqFile.write(each)
                # qqFile.write(";")
                flag=qqFile
            elif isWeichat(each):
                if weichatFile==None:
                    weichatFile=fileObj.fileCreate('weichat')
                weichatFile.write("\n")
                weichatFile.write(each)
                # weichatFile.write(";")
                flag=weichatFile
            elif isEmail(each):
                if emailFile==None:
                    emailFile=fileObj.fileCreate('email')
                emailFile.write("\n")
                emailFile.write(each)
                # emailFile.write(";")
                flag=emailFile
            elif isAddress(each):
                if addressFile==None:
                    addressFile=fileObj.fileCreate('address')
                addressFile.write("\n")
                addressFile.write(each)
                # addressFile.write(";")
                flag=addressFile
            elif isMobilphoneNumber(each):
                if phoneFile==None:
                    phoneFile=fileObj.fileCreate('phone')
                phoneFile.write("\n")
                phoneFile.write(each)
                # phoneFile.write(";")
                flag=phoneFile
            elif isTelphoneNumber(each):
                if telFile==None:
                    telFile=fileObj.fileCreate('telphone')
                telFile.write("\n")
                telFile.write(each)
                # telFile.write(";")
                flag=telFile
            elif isUrl(each):
                if urlFile==None:
                    urlFile=fileObj.fileCreate('url')
                urlFile.write("\n")
                urlFile.write(each)
                # urlFile.write(" ")
                flag=urlFile
            elif isOther(each):
                if otherFile==None:
                    otherFile=fileObj.fileCreate('other')
                otherFile.write("\n")
                otherFile.write(each)
                # urlFile.write(" ")
                flag=otherFile
            else:
                if flag !=None:
                    flag.write("\n")
                    flag.write(each)
        #文件关闭操作
        fileObj.fileClose(qqFile)
        fileObj.fileClose(addressFile)
        fileObj.fileClose(weichatFile)
        fileObj.fileClose(emailFile)
        fileObj.fileClose(phoneFile)
        fileObj.fileClose(telFile)
        fileObj.fileClose(urlFile)
        fileObj.fileClose(nameFile)
        fileObj.fileClose(otherFile)

    def countMark(self,context):
        # 后续需要添加表示字符识别
        count=0
        for item in context:
            if self.isIncludetarget(item,u'地址') or self.isIncludetarget(item,u'邮箱') or self.isIncludetarget(item,u'网址') or self.isIncludetarget(item,u'QQ') or self.isIncludetarget(item,u'手机') or self.isIncludetarget(item,u'座机') or self.isIncludetarget(item,u'微信') or self.isIncludetarget(item,u'联系人'):
                count+=1
            else:
                continue
        # 控制一个模块有多少个标准名称
        if count >=2:
            return True
        else:
            return False
    def isIncludetarget(self,string,target):
        if target in string:
            return True
        else:
            return False

    def word2CustomInfoSegmen(self,dirpath,inputfile,parentDirMd5,catalog):
        context=self.readWord(dirpath+inputfile)
        self.customFenkuai(context,catalog,parentDirMd5)

    def customFenkuai(self,content,catalog,parentDirMd5):
        targetCount=len(catalog)

        flag=None
        count=1
        fileObj1=None
        fh1=None

        fileObj2=None
        fh2=None
        for row in content:
            linshi=row.strip()
            if count<targetCount:
                temp1=catalog[str(count)]
                temp2=catalog[str(count+1)]
                if flag==None and linshi != temp1[0]:
                    continue
                elif flag==None and linshi == temp1[0]:
                    makeDir(customInfoDir+"\\"+parentDirMd5,temp1[1])
                    flag="ok"
                    fileObj1=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+temp1[1],"")
                    fh1=fileObj1.fileCreate("fullInfo")
                    fh1.write(linshi)
                    fh1.write("\n")

                elif flag != None and linshi != temp2[0]:
                    fh1.write(linshi)
                    fh1.write("\n")

                elif flag != None and linshi == temp2[0]:
                    count+=1
                    fileObj1.fileClose(fh1)

                    makeDir(customInfoDir+"\\"+parentDirMd5,temp2[1])
                    flag="ok"
                    fileObj2=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+temp2[1],"")
                    fh2=fileObj2.fileCreate("fullInfo")
                    fh2.write(linshi)
                    fh2.write("\n")

                    fh1=fh2
                    fileObj1=fileObj2
            else:
                fh1.write(linshi)
                fh1.write("\n")

        fileObj1.fileClose(fh1)
        #处理分片信息
        self.customFenPian(customInfoDir+"\\"+parentDirMd5)

    def customFenPian(self,targetDir):
        targetFenkuaiDir=os.listdir(targetDir)
        for oneFenkuai in targetFenkuaiDir:
            #读取分块内容
            content=None
            with open(targetDir+"\\"+oneFenkuai+"\\"+"fullInfo.txt") as f:
                content=f.readlines()

            fileObj=FileOperation(targetDir+"\\"+oneFenkuai,oneFenkuai)
            qqFile=None
            weichatFile=None
            emailFile=None
            addressFile=None
            phoneFile=None
            telFile=None
            urlFile=None
            nameFile=None
            otherFile=None
            flag=None

            for line in content:
                line=unicode(line.strip())

                #处理公司名称
                obj=startEnd()
                if isCompanyName(line,obj,regionInfo):

                    if nameFile==None:
                        nameFile=fileObj.fileCreate('companyName')

                    nameFile.write(obj.outputCompanyName(line))
                    nameFile.write("\n")
                    continue

                each=line
                # ----------------------------------------------
                if isQq(each):
                    if qqFile==None:
                        qqFile=fileObj.fileCreate('qq')
                    qqFile.write("\n")
                    qqFile.write(each)
                    # qqFile.write(";")
                    flag=qqFile
                elif isWeichat(each):
                    if weichatFile==None:
                        weichatFile=fileObj.fileCreate('weichat')
                    weichatFile.write("\n")
                    weichatFile.write(each)
                    # weichatFile.write(";")
                    flag=weichatFile
                elif isEmail(each):
                    if emailFile==None:
                        emailFile=fileObj.fileCreate('email')
                    emailFile.write("\n")
                    emailFile.write(each)
                    # emailFile.write(";")
                    flag=emailFile
                elif isAddress(each):
                    if addressFile==None:
                        addressFile=fileObj.fileCreate('address')
                    addressFile.write("\n")
                    addressFile.write(each)
                    # addressFile.write(";")
                    flag=addressFile
                elif isMobilphoneNumber(each):
                    if phoneFile==None:
                        phoneFile=fileObj.fileCreate('phone')
                    phoneFile.write("\n")
                    phoneFile.write(each)
                    # phoneFile.write(";")
                    flag=phoneFile
                elif isTelphoneNumber(each):
                    if telFile==None:
                        telFile=fileObj.fileCreate('telphone')
                    telFile.write("\n")
                    telFile.write(each)
                    # telFile.write(";")
                    flag=telFile
                elif isUrl(each):
                    if urlFile==None:
                        urlFile=fileObj.fileCreate('url')
                    urlFile.write("\n")
                    urlFile.write(each)
                    # urlFile.write(" ")
                    flag=urlFile
                elif isOther(each):
                    if otherFile==None:
                        otherFile=fileObj.fileCreate('other')
                    otherFile.write("\n")
                    otherFile.write(each)
                    # urlFile.write(" ")
                    flag=otherFile
                else:
                    if flag !=None:
                        flag.write("\n")
                        flag.write(each)
            #文件关闭操作
            fileObj.fileClose(qqFile)
            fileObj.fileClose(addressFile)
            fileObj.fileClose(weichatFile)
            fileObj.fileClose(emailFile)
            fileObj.fileClose(phoneFile)
            fileObj.fileClose(telFile)
            fileObj.fileClose(urlFile)
            fileObj.fileClose(nameFile)
            fileObj.fileClose(otherFile)

        pass


if __name__=='__main__':
    w2t=Word2Txt()
    wordDir='./testData'

    for each in os.listdir(wordDir):
        w2t.word2Txt(wordDir+"/",each)























#import docx

#输入要提取文本信息的word文档的路径和名称
# inputFile="ceshi.docx"
#
# document = docx.Document(inputFile)
# # 设置提取信息输出的文件路径及文件名
# outputFile=".txt"
# output=open(outputFile,'a')
# # 进行信息提取
# for para in document.paragraphs:
#     print para.text
#
#     # print "-------------------------------"
#     output.write(para.text.encode('utf-8'))
#     output.flush()
# output.close()